package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.food.mapper.GoodsTypeFoodMapper;
import com.dycx.food.dto.GoodsTypeDTO;
import com.dycx.food.entity.GoodsTypeDO;
import com.dycx.food.enums.GoodsNatureEnum;
import com.dycx.food.service.GoodsTypeFoodService;
import com.dycx.food.vo.GoodsTypeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class GoodsTypeFoodImpl extends ServiceImpl<GoodsTypeFoodMapper, GoodsTypeDO> implements GoodsTypeFoodService {

    private GoodsTypeFoodMapper goodsTypeMapper;

    public Boolean delete(int goodsTypeId) {
        return this.removeById(goodsTypeId);
    }

    /**
     * 添加
     *
     * @param goodsTypeDto
     * @return
     */
    @Override
    public Boolean insert(GoodsTypeDTO goodsTypeDto) {
        GoodsTypeDO goodsTypeDO = BeanUtil.toBean(goodsTypeDto, GoodsTypeDO.class);
        goodsTypeDto.setId(goodsTypeDO.getId());
        return this.save(goodsTypeDO);
    }

    /**
     * 添加/修改
     *
     * @param goodsTypeDto
     * @return
     */
    @Override
    public Boolean update(GoodsTypeDTO goodsTypeDto) {
        GoodsTypeDO goodsTypeDO = BeanUtil.toBean(goodsTypeDto, GoodsTypeDO.class);
        return this.updateById(goodsTypeDO);
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<GoodsTypeVO> getPage(GoodsTypeDTO dto) {
        Page<GoodsTypeVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());  // 查询第1页，每页返回5条

        Page<GoodsTypeVO> pageRes = goodsTypeMapper.getPage(page, dto);

        pageRes.getRecords().forEach(item -> {
            item.setGoodsNatureName(GoodsNatureEnum.getCode(item.getGoodsNature()).getName());
        });

        return pageRes;
    }

    /**
     * 获取所有列表
     *
     * @return
     */
    @Override
    public List<GoodsTypeVO> getAllList() {
        List<GoodsTypeDO> goodsTypeDOList = this.list();

        return goodsTypeDOList.stream().map(item ->
                BeanUtil.toBean(item, GoodsTypeVO.class)
        ).collect(Collectors.toList());
    }

}
